﻿
Public Class ChatUser
    Property ID As Long
    Property Name As String
    Property IsOnline As Boolean

    Function getMessages()

    End Function

End Class

Public Class ChatManager

    Shared _lstMessages As List(Of ChatMessage)
    Shared ReadOnly Property Messages As List(Of ChatMessage)
        Get
            If _lstMessages Is Nothing Then
                _lstMessages = New List(Of ChatMessage)
            End If
            Return _lstMessages
        End Get
    End Property

    Private Shared _lstWaitingChatMessages As System.Collections.Generic.Dictionary(Of Long, System.Threading.EventWaitHandle)
    Shared ReadOnly Property WaitingChatMessages As System.Collections.Generic.Dictionary(Of Long, System.Threading.EventWaitHandle)
        Get
            If _lstWaitingChatMessages Is Nothing Then
                _lstWaitingChatMessages = New System.Collections.Generic.Dictionary(Of Long, System.Threading.EventWaitHandle)
            End If
            Return _lstWaitingChatMessages
        End Get
    End Property

    'Dim _lstChats As List(Of Chat)
    'ReadOnly Property Chats As List(Of Chat)
    '    Get
    '        If Me._lstChats Is Nothing Then
    '            Me._lstChats = New List(Of Chat)
    '        End If
    '        Return Me._lstChats
    '    End Get
    'End Property

    Public Shared Sub PostChat(ByVal msg As ChatMessage)
        PostChat(msg.UserFrom, msg.UserTo, msg.Message)
    End Sub

    Public Shared Sub PostChat(ByVal objUserFrom As ChatUser, ByVal objUserTo As ChatUser, ByVal msg As String)

        Dim o As New ChatMessage
        o.Message = msg
        o.UserFrom = objUserFrom
        o.UserTo = objUserTo

        Messages.Add(o)

        If WaitingChatMessages.ContainsKey(objUserTo.ID) Then

            Dim objWaitHandle = WaitingChatMessages.Item(objUserTo.ID)
            If objWaitHandle IsNot Nothing Then
                objWaitHandle.Set()
            End If

        End If

    End Sub

    Public Shared Function GetMessages(ByVal userID As Long, ByVal timeStamp As Long) As List(Of ChatMessage)
        Dim msgs As List(Of ChatMessage)

        msgs = (From t In Messages Where t.UserTo.ID = userID AndAlso t.Time.Ticks > timeStamp Select t).ToList

        If msgs.Count = 0 Then
            Dim o As New System.Threading.AutoResetEvent(True)
            WaitingChatMessages.Add(userID, o)
            o.Reset()

            o.WaitOne(30000)

            msgs = (From t In Messages Where t.UserTo.ID = userID AndAlso t.Time.Ticks > timeStamp Select t).ToList

            WaitingChatMessages.Remove(userID)

        End If

        Return msgs.ToList
    End Function

End Class

Public Class ChatMessage
    Property UserFrom As ChatUser
    Property UserTo As ChatUser
    Property Message As String
    Property Time As DateTime
    Sub New()
        Me.Time = Now
    End Sub
End Class

'Public Class Chat
'    Property WaitSignal As System.Threading.AutoResetEvent
'    Event OnNewMessage As EventHandler
'    Property UserTo As ChatUser
'    Property UserFrom As ChatUser
'End Class